#include "logindialog.h"
#include "ui_logindialog.h"

extern QPixmap pixmapToRound(QPixmap &src, int radius);
extern QString sysType;
extern QString loginUser;

LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);

    // 设置默认标签
    ui->tabWidget->setCurrentIndex(0);
    connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(slotChangeTab(int)));

    // 设置输入为密码框
    ui->lineEdit_passwd_reg->setEchoMode(QLineEdit::Password);
    ui->lineEdit_passwd_regr->setEchoMode(QLineEdit::Password);
    ui->lineEdit_passwd_login->setEchoMode(QLineEdit::Password);
    // 设置只能输入数字和字母
    QRegExpValidator *pRevalidotor = new QRegExpValidator(QRegExp("[a-zA-Z0-9]{25}"), this);
    ui->lineEdit_user_reg->setValidator(pRevalidotor);
    ui->lineEdit_user_login->setValidator(pRevalidotor);
    ui->lineEdit_passwd_reg->setValidator(pRevalidotor);
    ui->lineEdit_passwd_regr->setValidator(pRevalidotor);
    ui->lineEdit_passwd_login->setValidator(pRevalidotor);
    // 设置输入提示
    ui->lineEdit_user_reg->setPlaceholderText("请输入用户名");
    ui->lineEdit_user_login->setPlaceholderText("请输入用户名");
    ui->lineEdit_passwd_regr->setPlaceholderText("请设置密码");
    ui->lineEdit_passwd_reg->setPlaceholderText("请确认密码");
    ui->lineEdit_passwd_login->setPlaceholderText("请输入密码");

    if(sysType=="osx")
        head = "../../../../res/title.jpeg";
    else
        head = "../res/title.jpeg";
    // 找到图片
    QPixmap px(":/res/title.jpeg"); // 缩放
    QPixmap dst = px.scaled(50,50,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    dst = pixmapToRound(dst, 25);
    ui->label_pix->setPixmap(dst);
    // 为按钮添加点击
    //ui->btn_login->setEnabled(false);
    connect(ui->btn_login, SIGNAL(clicked()), this, SLOT(slotLogin()));
    connect(ui->btn_cancle, SIGNAL(clicked()), this, SLOT(slotCancle()));
    connect(ui->btn_change, SIGNAL(clicked()), this, SLOT(slotChangeHead()));

    // 设置提示信息的颜色
    QPalette palette;
    palette.setColor(QPalette::WindowText, Qt::red);
    ui->label_tip->setPalette(palette);

    // 清空数据
    username.clear();
    password.clear();
    passwordr.clear();
    msg.clear();
}
// 登录按钮
void LoginDialog::slotLogin()
{
    if(ui->tabWidget->currentIndex() == 0)
    {
        // 获取用户名和密码
        username = ui->lineEdit_user_login->text();
        password = ui->lineEdit_passwd_login->text();
        if(username.length()>=3 && password.length()>=6)
        {
            // 包装信息
            msg = "#MSGSTART#login#" + username + "#" + password + "#MSGEND#";
            // 发射信号到上层
            emit btnClicked(msg);
            // 关闭对话框
            this->close();
        }
        else
        {
            ui->label_tip->setText("请输入正确的用户名和密码");
        }
    }
    else
    {
        // 获取用户名和密码
        username = ui->lineEdit_user_reg->text();
        password = ui->lineEdit_passwd_reg->text();
        passwordr = ui->lineEdit_passwd_regr->text();
        if(username.length()>=3 && password.length()>=6 && passwordr.length()>=6 && password==passwordr)
        {
            // 包装信息
            msg = "#MSGSTART#reg#" + username + "#" + password + "#" + head + "#MSGEND#";
            // 发射信号到上层
            emit btnClicked(msg);
            // 关闭对话框
            this->close();
        }
        else
        {
            ui->lineEdit_user_reg->setFocus();
            ui->lineEdit_passwd_reg->clear();
            ui->lineEdit_passwd_regr->clear();
            ui->lineEdit_user_reg->clear();
            if(username.length()<3)
            {

                ui->label_tip->setText("用户名长度不够，至少3个字符");
            }
            if(password.length()<6)
            {
                ui->label_tip->setText("密码长度不够，至少6个字符");
            }
            if(password != passwordr)
            {
                ui->label_tip->setText("两次输入的密码不一样");
            }
        }
    }
}

void LoginDialog::changeTab(int idx)
{
    if(idx==0)
        ui->btn_login->setText("登录");
    else
        ui->btn_login->setText("注册");
}
// 取消按钮
void LoginDialog::slotCancle()
{
    msg.clear();
    // 关闭对话框
    this->close();
}
// 切换tab
void LoginDialog::slotChangeTab(int idx)
{
    if(idx==0)
        ui->btn_login->setText("登录");
    else
        ui->btn_login->setText("注册");
}
// 更改头像
void LoginDialog::slotChangeHead()
{
    head = QFileDialog::getOpenFileName(this, tr("打开图片"), "/", tr("图片(*.png *.jpeg *.jpg)"));
    QPixmap px(head);
    QPixmap dst = px.scaled(50,50,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    dst = pixmapToRound(dst, 25);
    ui->label_pix->setPixmap(dst);
}

QString LoginDialog::getUser()
{
    return this->username;
}


LoginDialog::~LoginDialog()
{
    delete ui;
}
